package com.yonyou.service.demo;

import com.yonyou.service.demo.service.APPIdService;
import com.yonyou.service.demo.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.yonyou.service.demo.*"})
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Autowired
    APPIdService appIdService;

    @Override
    public void run(String... args) throws Exception {
        if (args.length > 0) {
            if ("-b".equals(args[0])) { // 创建APP ID
                System.out.printf("APPID: %s\n", appIdService.buildAppId());
                System.exit(0);
            }

            if ("-g".equals(args[0])) { // 生成 RSA 公私钥
                String[] pair = Utils.genKeyPair();
                System.out.printf("Public Key: %s\nPrivate Key: %s\n", pair[0], pair[1]);
                System.exit(0);
            }

            if (args.length > 1 && "-t".equals(args[0]) && !Utils.isEmptyString(args[1])) { // 测试APP ID
                String appId = args[1];
                try {
                    System.out.println(appIdService.checkAppId(appId) ? String.format("%s is valid appid", appId) : "");
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                System.exit(0);
            }
        }
    }
}
